import utils from "../../common/utils/utils";
import request from "../../common/utils/request";
import * as storage from "../../common/utils/storage";

import { baseSubtitle } from "@/views/shotstack/MuseMixedCut/utools/index.js";

import {
  global_logo_config
} from "@/views/shotstack/MixedCutPlatforms/utools/index.js";

const videoMixCut = {
  namespaced: true,
  state: {
    // 视频混剪参数
    VideoMashup: {},

    // 字幕与配音抽屉中维护的数据
    Subtitles: [],
    // 全局全局字幕配音中维护的数据
    Global_tts_config: {
      type: 0, // 1-使用全局字幕配音，0-不使用全局字幕配音
      configs: [
        {
          tab_id: "",
          audio_material_id: "",
          audio_url: "",
          iccts: [
            {
              elements: [
                {
                  type: "",
                  text: "",
                  content: "",
                  ph: "",
                  breakTime: "",
                  interpretAs: "",
                },
              ],
              start_time: "",
              end_time: "",
            },
          ],
          showVoice: true,
          showSubtitle: true,
          tone_id: "",
          emotion_category: "",
          emotion_intensity: "",
          speed: "",
          volume: "",
          duration: "",
          start_time: "",
          subtitle: [
            {
              text: "",
              ssml: "",
              font: "",
              font_size: "",
              flower_id: "",
              bg_color: "",
              color: "",
              bold: "",
              italic: "",
              x: "",
              y: "",
              start_time: "",
              end_time: "",
              alignment: "",
              underline: "",
            },
          ],
        },
      ],
    },
    // 当前设置的镜头组
    currentIndex: {
      index: 0,
    },
    // 当前编辑的视频组tts_config的id
    current_tts_id: "",

    // 当前编辑的全局tts_id
    global_edit_tts_id: "",

    // 当前编辑的光标位置信息
    line_msg: {},

    // 字幕项当前播放video的状态
    VideoPlayingData: {
      isPlay: false, // 是否播放
      audio_url: "",
      reset: true, // 是否从头播放
      changeUrl: false, // 是否改变url
    },

    // 当前富文本正在编辑的 subtitle项的信息
    editSubtitleData: {
      // id: '',
      // text:''
    },

    // 当前用户选择选区的信息
    selectionsData: {
      // text: '; // 总的文本内容
      // selectedNodes: []; // 选择节点数量
    },

    // 数据是否发生变化
    VideoMashupChange: false,
    // 花字列表
    FlowerArr: [],
    // 阿里云花字列表
    FlowerAliArr: [],
    // 默认花字列表
    FlowerDefaultArr: null,

    // 阿里云默认花字
    FlowerDefault_Aly: null,

    // 默认的音频配置
    voice_config: null,

    // 推荐的默认配置
    volcengine_config: {},

    // 字体列表
    fontList: [],
    /**
     * @description: ai 生成图片素材 异步开关, 每次轮询都会判断这个开关是否是开启状态
     * true: 允许生成, false:结束轮询
     */
    AiCreatSwitch: false,

    /**
     * @description: 模板内 ai 生成图片素材 异步开关, 每次轮询都会判断这个开关是否是开启状态
     * true: 允许生成, false:结束轮询
     * @
     */

    TemplateAiCreatSwitch: false,
    // 全局字幕配音, 正在修改的tts项的id
    globalTtsCurrent_id: "",
    // 默认的音色
    defaultVoiceData: null,
    // 全部的音色列表
    allVoiceData: [],
    // 存放回显的文件夹id信息
    folder_ids: {},
  },
  mutations: {
    SET_VOLUME_DEFAULT(state, value) {
      state.voice_config.volume.default = value;
    },
    SET_VOLUME_DEFAULT_SPEED(state, value) {
      state.voice_config.speed.default = value;
    },
    SET_VOLUME_DEFAULT_INTENSITY(state, value) {

      console.log(state.voice_config,  '看看内容1111');
      state.voice_config.intensity.default = value;
    },
    // 存放回显的文件夹id信息
    SET_FOLDER_IDS: (state, data) => {
      state.folder_ids = data;
    },
    SET_WORK_AI_CONFIG: (state, data) => {
      state.work_ai_config = data;
    },
    SET_GLOBALTTS_CURRENT_ID: (state, id) => {
      state.globalTtsCurrent_id = id;
    },
    // 修改ai 生成素材轮询开关
    SET_AICREAT_SWITCH: (state, payload) => {
      state.AiCreatSwitch = payload;
    },
    // 修改模板 生成素材轮询开关
    SET_TemplateAiCreatSwitch_SWITCH: (state, payload) => {
      state.TemplateAiCreatSwitch = payload;
    },
    // 存放字体列表数据
    SET_FONTFAMILY_DATA: (state, payload) => {
      state.fontList = payload;
    },

    // 存放音频默认配置数据
    SET_DEFAULT_VOICE_DATA: (state, payload) => {
      state.defaultVoiceData = payload;
      // localStorage.setItem("defaultVoiceData", JSON.stringify(payload));
    },

    // 模板筛选条件
    SET_SELECT_TEMPLATE: (state, payload) => {
      Object.assign(state.selectTemplate, {
        ...payload,
      });
    },
    // 基础信息
    SET_CLIP_INFO: (state, payload) => {
      Object.assign(state.clipJSON, {
        ...payload,
      });
      console.log("CLIP_JSON", state.clipJSON);
    },
    SET_DRAG_STATUS: (state, object) => {
      return new Promise((resolve, reject) => {
        state.dragStatus = object;
        resolve();
      });
    },
    SET_EXPECTED_VIDEO: (state, object) => {
      Object.assign(state.expectedVideo, object);
      // state.expectedVideo = object;
    },
    SET_LIBRARY_ID: (state, libraryId) => {
      state.clipJSON.merchant_library_id = libraryId;
    },
    // 场景数据
    SET_CLIP_JSON_RESOURCE_DATA: (state, payload) => {
      Object.assign(state.clipJSON, {
        // resource_data: payload.map((item, index) => ({ sn: index, ...item })),
        resource_data: payload,
      });
      console.log("RESOURCE_DATA", state.clipJSON);
    },
    // bgm数据
    SET_CLIP_JSON_BGM_DATA: (state, payload) => {
      Object.assign(state.clipJSON, {
        bgm: payload,
      });
      console.log("BGM_DATA", state.clipJSON);
    },
    // 口播数据
    SET_CLIP_JSON_AUDIO_DATA: (state, payload) => {
      Object.assign(state.clipJSON, {
        audio: payload,
      });
      console.log("AUDIO_DATA", state.clipJSON);
    },
    // 音量数据
    SET_CLIP_JSON_VOLUME: (state, payload) => {
      Object.assign(state.clipJSON.volume, payload);
      console.log("VOLUME_DATA", state.clipJSON);
    },
    // 颜色数据
    SET_CLIP_JSON_COLOR: (state, payload) => {
      Object.assign(state.clipJSON.effect_filter.color, payload);
      console.log("ColorData", state.clipJSON);
    },
    // 特效数据
    SET_CLIP_JSON_EFFECTS: (state, payload) => {
      const effectIndex = state.clipJSON.effects.findIndex(
        (item) => item.type === payload.type
      );

      if (effectIndex >= 0) {
        if (payload.params) {
          state.clipJSON.effects.splice(effectIndex, 1, payload);
        } else {
          state.clipJSON.effects.splice(effectIndex, 1);
        }
      } else {
        if (payload.params) {
          state.clipJSON.effects.push(payload);
        }
      }

      console.log("EffectsData", state.clipJSON);
    },
    //文字数据
    SET_CLIP_JSON_WORD: (state, payload) => {
      state.clipJSON.whole_subtitle_config = payload;
    },
    //更新封面数据
    SET_CLIP_JSON_COVER: (state, payload) => {
      state.clipJSON.cover_config = payload;
    },
    //贴纸数据
    SET_CLIP_JSON_STICKER: (state, payload) => {
      state.clipJSON.sticker_config = payload;
    },
    //当前播放的预览视频
    SET_CRT_SHOW_VIDEO: (state, payload) => {
      state.crtShowVideo = payload;
    },
    // loading
    SET_GENERATE_LOADING: (state, payload) => {
      state.generateLoading = payload;
    },
    // 全局文本标题
    SET_GENERATE_GLOBALTITLE: (state, payload) => {
      state.VideoMashup.global_text_config.type = payload.value;
    },
    // 全局字幕配音
    SET_GENERATE_GLOBALVIDEO: (state, payload) => {
      state.VideoMashup.global_tts_config.type = payload.value;
    },
    // 全局转场摄者
    SET_GENERATE_GLOBALTran: (state, payload) => {
      state.VideoMashup.transition_config.type = payload.value;
    },
    // 全局滤镜
    SET_GENERATE_GLOBALFilter: (state, payload) => {
      state.VideoMashup.filter_config.type = payload.value;
    },
    // 全局背景色
    SET_GENERATE_GLOBALBg: (state, payload) => {
      state.VideoMashup.background.type = payload.value;
    },
    // 视频封面设置
    SET_GENERATE_GLOBALVideoBg: (state, payload) => {
      state.VideoMashup.cover_config.type = payload.value;
    },
    // 全局配乐
    SET_GENERATE_GLOBALBGM: (state, payload) => {
      state.VideoMashup.bgm_config.type = payload.value;
    },
    //组合预览列表
    SET_CRT_SHOW_VIDEO_LIST: (state, payload) => {
      state.crtShowVideoList = payload;
    },
    // 设置字幕
    SET_SUB_TITLE: (state, payLoad) => {
      state.VideoMashup.bgm_config.type = payLoad.value;
    },

    // 设置logo
    SET_LOGO_DATA: (state, payLoad) => {
      console.log(payLoad, "仓库 存");
      Object.assign(
        state.VideoMashup.global_logo_config.logo_urls[0],
        payLoad.logoData
      );
    },
    // 清空logo数据
    CLEAR_LOGO_DATA: (state) => {
      state.VideoMashup.global_logo_config = global_logo_config();
    },

    REFRASH_RESOURCE: (state, reload) => {
      state.reloadResource = reload;
    },
    UPDATE_RESOURCE_DATA: (state, payload) => {
      state.clipJSON.resource_data[payload.index].video = payload.data;
      console.log("UPDATE_RESOURCE_DATA", state.clipJSON);
    },

    // 会员权益
    SET_VERSION_INFO: (state, info) => {
      state.versionInfo = info;
    },
    // 设置脚本数据
    SET_SCRIPT_DATA: (state, data) => {
      state.clipJSON.script_data = data;
    },
    // 一键成片
    SET_AI_COMPOSE_CONFIG: (state, payload) => {
      Object.assign(state.clipJSON.ai_compose_config, payload);
      console.log("AiComposeConfigData", state.clipJSON);
    },

    // 初始化视频混剪数据
    SET_VIDEOMASHUP: (state, data) => {
      Object.assign(state.VideoMashup, { ...data });
      console.log("VideoMashup", state.VideoMashup);
    },
    // 修改某项数据
    SET_VIDEOMASHUP_ITEM: (state, data) => {
      console.log(state, "111111111111111111111");
      console.log(data, "222222222222");

      let type = Object.prototype.toString.call(data.value);
      type == "[object Object]"
        ? (() => {
            state.VideoMashup[data.key] = Object.assign(
              state.VideoMashup[data.key],
              data.value
            );
          })()
        : (state.VideoMashup[data.key] = data.value);

      console.log(state.VideoMashup, "修改之后的数据");
    },
    // 移除视频混剪数据
    REMOVE_VIDEOMASHUP_DATA: (state) => {
      state.VideoMashup = {};
    },

    // 初始化字幕与配音抽屉数据
    INIT_SUBTITLES_DATA: (state, data) => {
      // debugger
      state.Subtitles = JSON.parse(JSON.stringify(data));
    },
    // 修改某个抽屉中的开始时间
    MODIFY_DRAWER_START_TIME_DATA: (state, data) => {
      state.Subtitles[data.index].tts_config.forEach(
        (ele) => (ele[data.key] = data.value)
      );
      // state.Subtitles[data.index].tts_config[data.key] = data.value;
    },

    SETVIDEOPLAYING_DATA: (state, data) => {
      console.log(data, "修改值纸质纸盒子很自豪");
      let update = JSON.parse(JSON.stringify(state.VideoPlayingData));
      for (let k in data) {
        update[k] = data[k];
      }
      state.VideoPlayingData = update;
      // Object.assign({}, state.VideoPlayingData, data);
    },

    // 修改某个抽屉中的语音数据 - 并清空失效的音频数据
    MODIFY_DRAWER_VOICE: (state, data) => {
      const editType = state.Subtitles[state.currentIndex.index].tab_id;
      console.log(data, "修改的数据MODIFY_DRAWER_VOICE");
      state.Subtitles[data.index].tts_config.forEach((ele) => {
        if (editType == ele.tab_id) {
          ele[data.key] = data.value;
          if (data.key != "start_time") {
            ele.subtitle.forEach((ele) => {
              ele.audio_url = "";
              ele.duration = "";
            });
          }
        }
      });
      // state.Subtitles[data.index].tts_config[data.key] = data.value;
      console.log("修改某个抽屉中的语音数据", state.Subtitles);
    },

    // input 事件, 重置subtitle的某些数据
    MODIFY_DRAWER_SUBTITLE: (state, data) => {
      // const editType = state.Subtitles[state.currentIndex.index].tab_id
      state.Subtitles[state.currentIndex.index].tts_config.forEach((ele) => {
        // 当前编辑的tts_config
        if (ele.tts_id == data.tts_id) {
          const subtitleIndex = ele.subtitle.findIndex(
            (i) => i.subtitle_id == data.oldId
          );
          // 修改id, 清空音频数据, 修改ssml或者 对应的文本数据
          subtitleIndex != -1 &&
            (() => {
              ele.subtitle[subtitleIndex].ssml = data.ssml;
              ele.subtitle[subtitleIndex].text = data.text;
              ele.subtitle[subtitleIndex].subtitle_id = data.newId;
              ele.subtitle[subtitleIndex].audio_url = "";
              ele.subtitle[subtitleIndex].duration = "";
            })();
        }
      });
      // console.log('富文本修改仓库', state.Subtitles[state.currentIndex.index]);
    },

    // 往现在正在编辑的tts中, 添加一个 subtitle
    ADD_DRAWER_SUBTITLE: (state, data) => {
      const subtitle = baseSubtitle();
      state.Subtitles[state.currentIndex.index].tts_config.forEach((ele) => {
        if (ele.tts_id == state.current_tts_id) {
          ele.subtitle.push({
            ...subtitle,
            ...data,
          });
        }
      });
    },

    // state.Subtitles[data.index].tts_config[data.key] = data.value;

    // 存放当前编辑的tts_config的id
    MODIFY_TTS_ID: (state, data) => {
      state.current_tts_id = data;
    },

    // 存放当前编辑的全局tts_id
    MODIFY_GLOBAL_TTS_ID: (state, data) => {
      state.global_edit_tts_id = data;
    },

    // 存放当前编辑的富文本项的文本和id等各项信息
    SET_EDIT_SUBTITLE_DATA: (state, data) => {
      state.editSubtitleData = data;
    },

    // 存放当前节点的选区信息
    SET_SELECTION_DATA: (state, data) => {
      state.selectionsData = data;
    },

    // 移除字幕与配音数据
    REMOVE_SUBTITLES_DATA: (state, data) => {
      state.Subtitles = {};
    },
    // 修改抽屉中的subtitle 下面的字体信息
    MODIFY_SUBTITLES_DATA: (state, data) => {
      // debugger
      const editType = state.Subtitles[state.currentIndex.index].tab_id;
      state.Subtitles[state.currentIndex.index].tts_config.forEach((ele) => {
        // 如果编辑的类型相同, 修改此类型的值
        if (ele.tab_id == editType) {
          ele.subtitle.forEach((i) => {
            // i.material_path = data.material_path
            i.font_size = data.font_size;
            i.alignment = data.alignment;
            i.font_family = data.font_family;
            i.font_name = data.font_name;
            i.font_url = data.font_url;
            i.font_style = data.font_style;
            i.font_path = data.font_path;
            i.flower_id = data.flower_id;
            i.bg_color = data.bg_color;
            i.color = data.color;
            i.bold = data.bold;
            i.flower_config = JSON.parse(JSON.stringify(data.flower_config));
            i.preset_styles_config = JSON.parse(
              JSON.stringify(data.preset_styles_config)
            );
          });
        }
      });

      console.log("修改抽屉中的subtitle 下面的字体信息", state.Subtitles);
    },
    // 修改抽屉中的subtitle 下面的字体位置信息
    MODIFY_SUBTITLES_POSITION: (state, data) => {
      const editType = state.Subtitles[state.currentIndex.index].tab_id;
      state.Subtitles[state.currentIndex.index].tts_config.forEach((ele) => {
        if (ele.tab_id == editType) {
          ele.subtitle.forEach((i) => {
            // 如果是本项 则等于自己的x
            if (i.subtitle_id == data.id) {
              i.x = data.x;
              i.rightX = data.rightX;
            } else {
              const x = data.showX - (i.showX - i.x);
              const rightX = data.showX + (i.showX - i.x);
              i.x = x < 0 ? 0 : x;
              i.rightX = rightX;
            }
            i.y = data.y;
            i.showX = data.showX;
            // data.spanWidth && (i.spanWidth = data.spanWidth)
          });
        }
      });
      console.log(state.Subtitles, "修改后的state.Subtitles");
    },

    // 修改全局字幕抽屉中的文字位置信息
    MODIFY_GLOBAL_SUBTITLES_POSITION: (state, data) => {
      state.Global_tts_config.configs.forEach((ele) => {
        if (ele.tab_id == data.configs_type) {
          ele.subtitle.forEach((item) => {
            if (item.subtitle_id == data.id) {
              item.x = data.x;
              item.rightX = data.rightX;
            } else {
              const x = data.showX - (item.showX - item.x);
              const rightX = data.showX + (item.showX - item.x);
              item.x = x < 0 ? 0 : x;
              item.rightX = rightX;
            }
            item.showX = data.showX;
            item.y = data.y;
          });
        }
      });
      console.log(state.Global_tts_config.configs, "修改全局位置", data);
    },

    // 修改抽屉中是是否开启字幕配置
    MODIFY_SHOW_SUBTITLE(state, data) {
      state.Subtitles[state.currentIndex.index].tts_config.forEach((ele) => {
        ele.showSubtitle = data;
      });
      console.log(
        state.Subtitles[state.currentIndex.index].tts_config,
        "修改字幕是否开启"
      );
    },

    // 修改视频组 花字tab栏
    MODIFY_FLOWER_TAB: (state, data) => {
      state.Subtitles[
        state.currentIndex.index
      ].flower_random_tts_config.flower_type = data;
    },

    // 修改视频组 是否随机
    MODIFY_FLOWER_ISRANDOM: (state, data) => {
      state.Subtitles[
        state.currentIndex.index
      ].flower_random_tts_config.isRandom = data;
    },

    // 修改抽屉中的 ttsConfig
    MODIFY_SUBTITLES_TTSCONFIG: (state, data) => {
      console.log(data, "保存音色");
      state.Subtitles[state.currentIndex.index]?.tts_config.forEach((ele) => {
        if (ele.tab_id == "text") {
          ele.emotion_category = data.emotion_category;
          (ele.speed = data.speed), // 语速
            (ele.recommendSpeed = data?.recommendSpeed), // 推荐语速
            (ele.volume = data.volume), // 音量
            (ele.tone_id = data.tone_id), // 选择的声音id
            (ele.id = data.id), // 选择的声音id
            (ele.emotion_intensity = data.emotion_intensity); //情感程度
          ele.emotion_category = data.emotion_category; //情感类型
          ele.voice_type = data?.voice_type; // 音色类型 2 为火山
          ele.img = data.img; //音色图片
          ele.name = data.name; //音色名称
          // 清除每一项的 音频缓存
          ele.subtitle.forEach((i) => {
            i.audio_url = "";
            i.duration = "";
            i.end_time = 0;
          });
          ele.id = data.id;
          ele.category = data.category;
        }
      });
    },

    // 修改抽屉中字幕的生成方式
    CHANGE_TTS_TAB_ID: (state, data) => {
      // state.Subtitles[state.currentIndex.index].tts_config.forEach((ele) => {
      //   ele.tab_id = data;
      // });
      state.Subtitles[state.currentIndex.index].tab_id = data;
    },
    // 切换镜头组index
    CHANGE_CURRENT_INDEX: (state, data) => {
      console.log(data, "indexdata");
      state.currentIndex = data;
      console.log(state.currentIndex, "indexindexindexindexindex");
    },
    // 修改 当前编辑的光标位置信息
    MODIFY_CURRENT_LINE_MSG: (state, data) => {
      state.line_msg = data;
    },
    // 初始化全局字幕与配音数据
    INIT_GLOBALTTSCONFIG_DATA: (state, data) => {
      console.log(data, "初始化全局数据");
      state.Global_tts_config = JSON.parse(JSON.stringify(data));
    },
    INIT_GLOBALTTSCONFIG_GLDATA: (state, data) => {
      // state.Global_tts_config = JSON.parse(JSON.stringify(data));
    },

    INIT_GLOBALTTSCONFIG_CONGIG: (state, data) => {
      state.VideoMashup.global_tts_config = JSON.parse(JSON.stringify(data));
    },

    // 修改全局字幕的生成方式
    CHANGE_GLOBALTTS_TAB_ID: (state, data) => {
      if (state.Global_tts_config.configs_type == "text") {
        state.Global_tts_config.configs_text.forEach((ele) => {
          ele.tab_id = data;
        });
      } else {
        state.Global_tts_config.configs_audio.forEach((ele) => {
          ele.tab_id = data;
        });
      }
    },
    // 移除全局字幕与配音数据
    REMOVE_GLOBALTTSCONFIG_DATA: (state, data) => {
      state.Global_tts_config = {};
    },
    // 修改全局字幕配音
    MODIFY_GLOBALTTSCONFIG_VOICE: (state, data) => {
      const type = state.Global_tts_config.configs_type;
      state.Global_tts_config.configs.forEach((ele) => {
        if (ele.tab_id == type) {
          // 赋值对应数据, 置空失效的音频
          ele[data.key] = data.value;
          if (data.key != "start_time") {
            ele.subtitle.forEach((ele) => {
              ele.audio_url = "";
              ele.duration = "";
            });
          }
        }
      });
    },
    // 修改是否随机选项
    MODIFY_GLOBALTTSCONFIG_RANDOM: (state, data) => {
      state.Global_tts_config.isRandom = data;
    },

    // 修改抽屉中是是否开启全局字幕配置
    MODIFY_SHOW_GLOBALSUBTITLE(state, data) {
      const type = state.Global_tts_config.configs_type;
      state.Global_tts_config.configs.forEach((ele) => {
        ele.tab_id == type && (ele.showSubtitle = data);
      });
    },
    // 修改抽屉中的subtitle 下面的字体信息
    MODIFY_GLOBALSUBTITLES_DATA: (state, data) => {
      const type = state.Global_tts_config.configs_type;
      // debugger

      state.Global_tts_config.configs.forEach((ele) => {
        if (ele.tab_id == type) {
          ele.subtitle.forEach((i) => {
            // i.material_path = data.material_path;
            i.font_size = data.font_size;
            i.alignment = data.alignment;
            i.font_family = data.font_family;
            i.font_name = data.font_name;
            i.font_url = data.font_url;
            i.font_style = data.font_style;
            i.font_path = data.font_path;
            i.flower_id = data.flower_id;
            i.flower_config = data.flower_config;
            i.bg_color = data.bg_color;
            i.color = data.color;
            i.bold = data.bold;
            i.flower_config = JSON.parse(JSON.stringify(data.flower_config));
            i.preset_styles_config = JSON.parse(
              JSON.stringify(data.preset_styles_config)
            );
          });
        }
      });
    },
    // 修改全局抽屉中的 ttsConfig
    MODIFY_GLOSUBTITLES_TTSCONFIG: (state, data) => {
      // debugger
      state.Global_tts_config.configs.forEach((ele) => {
        if (ele.tab_id == "text") {
          ele.emotion_category = data.emotion_category;
          (ele.speed = data.speed), // 语速
            (ele.volume = data.volume), // 音量
            (ele.tone_id = data.tone_id), // 选择的声音id
            (ele.emotion_intensity = data.emotion_intensity); //情感程度
          ele.recommendSpeed = data?.recommendSpeed; // 推荐语速
          ele.emotion_category = data.emotion_category; //情感类型
          ele.id = data.id;
          ele.category = data.category;

          ele.img = data.img; //音色图片
          ele.name = data.name; //音色名称
          ele.selectVoice = JSON.parse(JSON.stringify(data.selectVoice));

          // 清除每一项的 音频缓存
          ele.subtitle.forEach((i) => {
            i.audio_url = "";
            i.duration = "";
            i.end_time = 0;
          });
        }
      });
    },

    // 改变数据变化的状态
    SYNCHRONIZATION_VIDEOMASHUP_CHANGE(state, data) {
      state.VideoMashupChange = data;
      console.log(data, " 数据发生改变");
    },

    // 存储花字数据
    // SET_FLOWER_LIST_DATA(state, data) {
    //   state.FlowerList = data;
    // },
    // 存储花字数据
    SET_FLOWER_ARR_DATA(state, data) {
      state.FlowerArr = data;
    },

    // 储存阿里云花字数据
    SET_FLOWER_ARR_DATA_ALI(state, data) {
      state.FlowerAliArr = data;
    },
    // 存储默认花字
    SET_FLOWER_ARR_DATA_DEFAULT(state, data) {
      state.FlowerDefaultArr = data;
    },
    // 存储阿里云默认花字
    SET_FLOWER_ARR_DATA_ALY_DEFAULT(state, data) {
      state.FlowerDefault_Aly = data;
    },
    // 存放默认的音频配置
    SET_DEFAULT_VOICE_CONFIG(state, data) {
      state.voice_config = data;
    },

    // 存放推荐专用的音频配置
    SET_DEFAULT_VOICENGINE_CONFIG(state, data) {
      state.volcengine_config = data;
    },
    // 存放默认音色
    SET_DEFAULT_VOICE_DATA(state, data) {
      state.defaultVoiceData = data;
      // localStorage.setItem("defaultVoiceData", JSON.stringify(data));
    },
    // 存放所有的音色列表
    SET_ALL_VOICE_DATA(state, data) {
      state.allVoiceData = data;
    },
  },
  actions: {
    Getwork_ai_config({ commit }, payload) {
      return new Promise((resolve, reject) => {
        try {
          request
            .get("/work_ai", "/config")
            .then((res) => {
              commit("SET_WORK_AI_CONFIG", res.list);
              setTimeout(() => {}, 0);
              resolve(res.list);
            })
            .catch((error) => {
              reject(error);
            });
        } catch (error) {
          console.log(error);

          reject(error);
        }
      });
    },
    SetClipInfo({ commit }, payload) {
      commit("SET_CLIP_INFO", payload);
    },
    SetDragStatus({ commit }, object) {
      commit("SET_DRAG_STATUS", object);
    },
    SetExpectedVideo({ commit }, object) {
      commit("SET_EXPECTED_VIDEO", object);
    },
    SetLibraryId({ commit }, libraryId) {
      commit("SET_LIBRARY_ID", libraryId);
    },
    SetClipJSONEffects({ commit }, payload) {
      commit("SET_CLIP_JSON_EFFECTS", payload);
    },
    SetClipJSONResourceData({ commit }, payload) {
      commit("SET_CLIP_JSON_RESOURCE_DATA", payload);
    },
    SetClipJSONBgmData({ commit }, payload) {
      commit("SET_CLIP_JSON_BGM_DATA", payload);
    },
    SetClipJsonAudioData({ commit }, payload) {
      commit("SET_CLIP_JSON_AUDIO_DATA", payload);
    },
    SetClipJSONVolume({ commit }, payload) {
      commit("SET_CLIP_JSON_VOLUME", payload);
    },
    SetClipJSONColor({ commit }, payload) {
      commit("SET_CLIP_JSON_COLOR", payload);
    },
    SetClipJSONWord({ commit }, payload) {
      commit("SET_CLIP_JSON_WORD", payload);
    },
    SetClipJSONCover({ commit }, payload) {
      commit("SET_CLIP_JSON_COVER", payload);
    },
    SetClipJSONSticker({ commit }, payload) {
      commit("SET_CLIP_JSON_STICKER", payload);
    },
    SetCrtShowVideo({ commit }, payload) {
      commit("SET_CRT_SHOW_VIDEO", payload);
    },
    SetGenerateLoading({ commit }, payload) {
      commit("SET_GENERATE_LOADING", payload);
    },
    SetGenerateGlobalTitle({ commit }, payload) {
      commit("SET_GENERATE_GLOBALTITLE", payload);
    },
    SetGenerateGlobalVideo({ commit }, payload) {
      commit("SET_GENERATE_GLOBALVIDEO", payload);
    },
    SetGenerateGlobalTran({ commit }, payload) {
      commit("SET_GENERATE_GLOBALTran", payload);
    },
    SetGenerateGlobalFilter({ commit }, payload) {
      commit("SET_GENERATE_GLOBALFilter", payload);
    },
    SetGenerateGlobalBg({ commit }, payload) {
      commit("SET_GENERATE_GLOBALBg", payload);
    },
    SetGenerateGlobalVideoBg({ commit }, payload) {
      commit("SET_GENERATE_GLOBALVideoBg", payload);
    },
    SetGenerateGlobalBgm({ commit }, payload) {
      commit("SET_GENERATE_GLOBALBGM", payload);
    },
    SetCrtShowVideoList({ commit }, payload) {
      commit("SET_CRT_SHOW_VIDEO_LIST", payload);
    },
    SetSubTitle({ commit }, payLoad) {
      commit("SET_SUB_TITLE", payLoad);
    },
    RefrashResource({ commit }, reload) {
      commit("REFRASH_RESOURCE", reload);
    },
    UpdateResourceData({ commit }, payLoad) {
      commit("UPDATE_RESOURCE_DATA", payLoad);
    },
    GetVersionInfo: ({ commit }) => {
      return new Promise((resolve) => {
        request
          .get("/work_ai", "/user/shotstack/version/info")
          .then((res) => {
            commit("SET_VERSION_INFO", res?.result);
            resolve(res?.result);
          })
          .catch(() => {
            resolve();
          });
      });
    },

    SetScriptData: ({ commit }, data) => {
      commit("SET_SCRIPT_DATA", data);
    },
    // 一键成片
    SetAiComposeConfigData: ({ commit }, payload) => {
      commit("SET_AI_COMPOSE_CONFIG", payload);
    },
    SetSelectTemplate: ({ commit }, payload) => {
      commit("SET_SELECT_TEMPLATE", payload);
    },

    // 初始化视频混剪数据
    SetVideoMashup: ({ commit }, payload) => {
      commit("SET_VIDEOMASHUP", payload);
    },

    // 修改某项视频混剪数据
    SetVideoMashupItem: ({ commit }, payload) => {
      console.log(payload, "11111111111dwdwdwdw");

      // debugger;

      commit("SET_VIDEOMASHUP_ITEM", payload);
    },
    // 移除视频混剪数据
    RemoveVideoMashupData: ({ commit }) => {
      commit("REMOVE_VIDEOMASHUP_DATA");
    },
    // 获取花字数据
    getFlowerListData: async ({ commit }) => {
      // const res = await request.get("/work_ai", "/user/shotstack/material/flower/list")
      const presetStyles = await request.get(
        "/work_ai",
        "/user/shotstack/material/presetStyles/list"
      );
      // commit("SET_FLOWER_LIST_DATA", res?.result);
      // 存放花字列表
      commit("SET_FLOWER_ARR_DATA", presetStyles?.result);

      // 存放阿里云花字列表
      commit("SET_FLOWER_ARR_DATA_ALI", presetStyles?.resultEffect);

      // 存放默认的系统花字
      commit("SET_FLOWER_ARR_DATA_DEFAULT", presetStyles?.default);

      const is_default = presetStyles?.resultEffect.find(
        (ele) => ele.is_default == 1
      );

      // 存放默认的 阿里云花字
      commit("SET_FLOWER_ARR_DATA_ALY_DEFAULT", is_default || {});

      console.log(presetStyles, "花字列表arr");
    },
    // 获得默认音频配置数据
    getDefaultVoiceData: ({ commit, state }) => {
      return new Promise(async (resolve, reject) => {
        if(state.voice_config) {
          resolve(state.voice_config);
          return; 
        }
        const res = await request.get(
          "/work_ai",
          "/user/shotstack/voice/config/get"
        );
        commit("SET_DEFAULT_VOICE_CONFIG", res?.voice_config);
        commit("SET_DEFAULT_VOICENGINE_CONFIG", res?.volcengine_config);
        resolve(res?.voice_config);
        console.log(res, "默认音频配置");
      });
    },
    // 获取字体数据
    getFontFamily: async ({ commit }) => {
      return new Promise((resolve, reject) => {
        try {
          request
            .get("/work_ai", "/user/shotstack/material/fonts/list")
            .then((res) => {
              res.result.map((item) => {
                item.label = item.name;
                item.value = item.material_url;
              });
              commit("SET_FONTFAMILY_DATA", res.result);
              resolve();
              res.result.forEach((font) => {
                const style = document.createElement("style");
                style.innerHTML = `
              @font-face {
                font-family: '${font.font_family}';
                src: url('${font.material_url}') format('truetype');
              }
            `;
                document.head.appendChild(style);
              });
            })
            .catch((error) => {
              reject(error);
            });
        } catch (error) {
          reject(error);
        }
      });
    },

    // 获取默认音色
    getRequestVoiceData: async ({ commit, state }) => {
      return new Promise((resolve, reject) => {
        if(state.defaultVoiceData) {
          resolve(state.defaultVoiceData);
          return;
        }
        try {
          request
            .get("/work_ai", "/user/shotstack/clip/default")
            .then((res) => {
              commit("SET_DEFAULT_VOICE_DATA", res.result.voice_config);
              resolve();
            })
            .catch((err) => {
              console.log(err);
              reject(err);
            });
        } catch (error) {}
      });
    },
    // 获取全部的音色列表(用于随机)
    getAllVoiceList: async ({ commit }) => {
      return new Promise((resolve, reject) => {
        try {
          request
            .get("/work_ai", "/user/shotstack/voice/all")
            // .get("/work_ai", "/user/shotstack/voice/list")
            .then((res) => {
              // const arr = [res.result.map((ele) => ele.item)].flat(Infinity);
              console.log(res.result, "全部的音色");

              // console.log(arr, "处理平面的音色-------------");
              commit("SET_ALL_VOICE_DATA", res.result);
              resolve(res.result);
            })
            .catch((err) => {
              console.log(err);
              reject(err);
            });
        } catch (error) {}
      });
    },
  },
  getters: {
    // 判断是否所有视频组的视频不为空
    allVideoIsEmpty(state, payload) {
      return state.VideoMashup?.resource_data?.every((item) => {
        let filterMaterialList = item.material_list.filter((ele) => {
          return ele.material_url != "";
        });
        return filterMaterialList.length !== 0;
      });
    },
  },
};

export default videoMixCut;
